package com.spring.ocrm.web.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.ocrm.web.dao.OCRMMapper;
import com.spring.ocrm.web.model.Order;
import com.spring.ocrm.web.model.ReturnOrder;
import com.spring.ocrm.web.model.ReturnOrderDetail;
import com.spring.ocrm.web.service.OCRMService;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.SsoMapper;

@Service
public class OCRMServiceImpl implements OCRMService{
	
	@Resource 
	private JedisCache jedisCache;
	
	@Resource 
	private SsoMapper ssoMapper;	
	
	@Resource 
	private OCRMMapper ocrmapper;	
	
	@Override
	public BaseResult stockin(String productStockinID,HttpServletRequest request)throws Exception {		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		
		/*		int EntityAvailableState = ocrmapper.getProductStockinStatus(productStockinID);
		if(EntityAvailableState==10){
			return new BaseResult("0","不能重复使用该入库单");
		}*/
		int count=ocrmapper.checkIsOverStock(productStockinID);
		if(count>0){
			return new BaseResult("0","有商品超过仓库存储上限");
		}		
		String InitialOperatorID=oaUser.getOa_user_id();
		String InitialOperatorName=oaUser.getOa_user_name();
		
		HashMap<String,Object> hm=new HashMap<String,Object>();
		hm.put("InitialOperatorID", InitialOperatorID);
		hm.put("InitialOperatorName", InitialOperatorName);
		hm.put("productStockinID", productStockinID);

		ocrmapper.insertStorehouseProduct(hm);
		
		ocrmapper.insertStockHistory(hm);
		
		int effect2=ocrmapper.updateStorehouseProduct(productStockinID);
		
		int effect3=ocrmapper.updateProductStockin(productStockinID);
		
		if(effect2<1||effect3<1){
			throw new Exception();
		}
		
		return new BaseResult("1","修改成功");		
	}	
	
	@Override
	public BaseResult stockout(String productStockoutID,HttpServletRequest request)throws Exception {		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		
		HashMap<String,Object> ps = ocrmapper.getProductStockoutStatus(productStockoutID);
		
//		int EntityAvailableState=(int)ps.get("EntityAvailableState");
		String stockOutType=MyTools.ObjectToString(ps.get("stockOutType"));
		
/*		if(EntityAvailableState==10){
			return new BaseResult("0","不能重复使用该出库单");
		}*/
		int count=ocrmapper.checkIsOverStockOut(productStockoutID);
		if(count>0){
			return new BaseResult("0","库存不足");
		}		
		String InitialOperatorID=oaUser.getOa_user_id();
		String InitialOperatorName=oaUser.getOa_user_name();
		
		HashMap<String,Object> hm=new HashMap<String,Object>();
		hm.put("InitialOperatorID", InitialOperatorID);
		hm.put("InitialOperatorName", InitialOperatorName);
		hm.put("productStockinID", productStockoutID);
		
		int effect3=1;
		ocrmapper.insertStockOutHistory(hm);
		if(stockOutType.equals("0")){
			effect3=ocrmapper.updateProductSaleableQty(productStockoutID);
		}		
		int effect2=ocrmapper.updateProductStockout(productStockoutID);
		
		int effect4=ocrmapper.updateProductStockoutStatus(productStockoutID);
		
		
		if(effect2<1||effect3<1||effect4<1){
			throw new Exception();
		}
		
		return new BaseResult("1","修改成功");		
	}	
	
	
	@Override
	public BaseResult allocate(String crmStorehouseAllocateID,HttpServletRequest request)throws Exception {		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		
//		int  EntityAvailableState= ocrmapper.getAllocateStatus(crmStorehouseAllocateID);
/*		if(EntityAvailableState==10){
			return new BaseResult("0","不能重复使用该出库单");
		}*/
		int count=ocrmapper.checkAllocateIsOverStockin(crmStorehouseAllocateID);
		if(count>0){
			return new BaseResult("0","有商品超过仓库存储上限");
		}		
		int count2=ocrmapper.checkAllocateIsOverStockout(crmStorehouseAllocateID);
		if(count2>0){
			return new BaseResult("0","库存不足");
		}
		
		String InitialOperatorID=oaUser.getOa_user_id();
		String InitialOperatorName=oaUser.getOa_user_name();
		
		HashMap<String,Object> hm=new HashMap<String,Object>();
		hm.put("InitialOperatorID", InitialOperatorID);
		hm.put("InitialOperatorName", InitialOperatorName);
		hm.put("crmStorehouseAllocateID", crmStorehouseAllocateID);
		
		ocrmapper.insertAllocatetStorehouseProduct(hm);
		
		int effect= ocrmapper.updateAllocateStorehouseProduct(crmStorehouseAllocateID);
		
		int effect2=ocrmapper.updateCrmStorehouseAllocate(crmStorehouseAllocateID);
		
		ocrmapper.insertAllocateOutHistory(hm);

		ocrmapper.insertAllocateInHistory(hm);
		
		if(effect<1||effect2<1){
			throw new Exception();
		}
		
		return new BaseResult("1","调拨成功");		
	}
	
	@Override
	public BaseResult returnOrder(ReturnOrder ro,HttpServletRequest request) throws Exception {		
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		String deptID=oaUser.getUser_dept_id();
		String deptName=oaUser.getUser_dept_name();
		String userID=oaUser.getOa_user_id();
		String userName=oaUser.getOa_user_name();
		
		String crmReturnOrderID=UUID.randomUUID().toString();
		ro.setCrmReturnOrderID(crmReturnOrderID);
		
		String returnOrderNumber=returnOrderNumber("ro");
		
		//String returnOrderNumber=UUID.randomUUID().toString();
		ro.setReturnOrderNumber(returnOrderNumber);	
		
		//product_stockinID入库单以后补上
		ro.setDeptID(deptID);
		ro.setDeptName(deptName);
		ro.setInitialOperatorID(userID);
		ro.setInitialOperatorName(userName);
		
		int effect=ocrmapper.insertReturnOrder(ro);
		if(effect<1){
			throw new Exception();
		}		
		
		for(ReturnOrderDetail rod:ro.getReturnOrderDetail()){
			rod.setCrm_return_orderID(crmReturnOrderID);
			int effect2=ocrmapper.insertReturnOrderDetail(rod);
			if(effect2<1){
				throw new Exception();
			}
		}		
		String productStockinID=UUID.randomUUID().toString();
		String stockInCode=UUID.randomUUID().toString();	
		
		HashMap<String,Object> hm=new HashMap<String,Object>();
		hm.put("productStockinID", productStockinID);
		hm.put("stockInCode", stockInCode);
		hm.put("crmReturnOrderID", crmReturnOrderID);
		
		if(ro.getIsNeedReturProduct()==1){
			int effect3=ocrmapper.insertProductStockin(hm);
			int effect4=ocrmapper.insertProductStockinDetail(hm);
			int effect5=ocrmapper.updateCrmReturnOrderStockinID(hm);
			if(effect3<1||effect4<1||effect5<1){
				throw new Exception();
			}	
		}
		
		HashMap<String,Object> hm2=new HashMap<String,Object>();
		hm2.put("crmReturnOrderID", crmReturnOrderID);
				
		return new BaseResult("1","申请成功",hm2);		
	}	
	
	@Override
	public BaseResult returnOrderExecute(String crmReturnOrderID,HttpServletRequest request)throws Exception {		
		//OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		
		HashMap<String,Object> hm= ocrmapper.getCrmReturnOrder(crmReturnOrderID);
		
		int status=(int)hm.get("status");
		String product_stockinID=MyTools.ObjectToString(hm.get("product_stockinID"));		
		
		if(status==1){  
			return new BaseResult("0","不能重复使用该退货单");
		}
		//退货单生效
		if(product_stockinID!=null){
			BaseResult br=this.stockin(product_stockinID,request);			
			if(!br.getStatus().equals("1")){
				throw new Exception();
			}
		}

		int count=ocrmapper.updateCrmReturnOrder(crmReturnOrderID);
		if(count<0){
			throw new Exception();
		}		
		return new BaseResult("1","生效成功");		
	}
	

	@Override
	public BaseResult returnRechargeCardExecute(String contractID,HttpServletRequest request)throws Exception {		
		//OaUser oaUser = ShopOaSessionUtil.getOaUser(request);			
		//String crmReturnOrderID=ocrmapper.getReturnIDByRechargeApplicationID(contractID);
		
		HashMap<String,Object> hm= ocrmapper.getReturnNeedRechargeCardIDs(contractID);

		String rechargecardID=MyTools.ObjectToString(hm.get("rechargecardID"));		
		//String contractID=MyTools.ObjectToString(hm.get("contractID"));
		String rechargeApplicationID=MyTools.ObjectToString(hm.get("rechargeApplicationID"));
		
		String crmReturnOrderID=ocrmapper.getReturnIDByRechargeApplicationID(rechargeApplicationID);
		
		int effect=ocrmapper.deleteRechargecard(rechargecardID);
		int effect2=ocrmapper.deleteRechargecardApplication(rechargeApplicationID);
		int effect3=ocrmapper.deletecontract(contractID);
		
		int count=ocrmapper.updateCrmReturnOrder(crmReturnOrderID);
		if(count<0){
			throw new Exception();
		}	
		
		if(effect<1||effect2<1||effect3<1){
			throw new Exception();
		}
		return new BaseResult("1","生效成功");		
	}
	@Override
	public BaseResult returnMemberCardExecute(String contractID,HttpServletRequest request)throws Exception {		
		//OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		
		HashMap<String,Object> hm=ocrmapper.getReturnIDByMemberApplicationID(contractID);
		
		String crmReturnOrderID=MyTools.ObjectToString(hm.get("crmReturnOrderID"));
		//String contractID=MyTools.ObjectToString(hm.get("contractID"));
		String membercardID=MyTools.ObjectToString(hm.get("membercardID"));
		String c_membercard_applicationID=MyTools.ObjectToString(hm.get("c_membercard_applicationID"));
		
		int hasRecharge=ocrmapper.checkHasRecharge(crmReturnOrderID);
		
		int count=ocrmapper.getReturnCount(crmReturnOrderID);
		
		BaseResult baseResult=null;
		if(count==0){
			
			baseResult=this.returnRechargeCardExecute(contractID,request);
			
			count--;
			
			if(!baseResult.getStatus().equals("1")){
				return baseResult;
			}
			
/*			if(count==0){
				return baseResult;
			}*/
		}	
		
/*		if(count==0){
			return new BaseResult("0","没有退货内容");
		}*/
		
		int count2=ocrmapper.getCardCount(c_membercard_applicationID);
		
		if(count==count2){
			ocrmapper.deleteMembercardApplication(c_membercard_applicationID);
			ocrmapper.deleteMembercard(membercardID);
		}
		
		ocrmapper.deletecontract(contractID);
		
		int effect = ocrmapper.deleteCardDetail(crmReturnOrderID);
		
		int effect3=ocrmapper.deleteApplicationPackage(crmReturnOrderID);

		int effect2=ocrmapper.updateCrmReturnOrder(crmReturnOrderID);
		if(effect<1||effect2<1||effect3<1){
			throw new Exception();
		}	
		return new BaseResult("1","生效成功");		
	}
	
	
	@Override
	public BaseResult orderDetail(String orderID)throws Exception {		
		//OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		List<Order> orderDetail=ocrmapper.orderDetail(orderID);

		return new BaseResult("1","查询成功",orderDetail);		
	}
	
	public String returnOrderNumber(String title)throws Exception {		
		//OaUser oaUser = ShopOaSessionUtil.getOaUser(request);		
		String count=ocrmapper.returnOrderNumber();
		String returnOrderNumber=title+count;
		return returnOrderNumber;
	}
	
}